from maze_env import Maze
from RL_brain import DeepQNetwork


def run_maze():
    step = 0  # 记录执行步数
    for episode in range(300):
        # initial observation
        observation = env.reset()

        while True:
            # fresh env
            env.render()

            # RL choose action based on observation
            action = RL.choose_action(observation)

            # RL take action and get next observation and reward
            observation_, reward, done = env.step(action)

            # 记忆库存储数据
            RL.store_transition(observation, action, reward, observation_)

            if (step > 200) and (step % 5 == 0):
                RL.learn(episode)

            # swap observation
            observation = observation_

            # break while loop when end of this episode
            if done:
                break
            step += 1
    print('game over')
    env.destroy()


# if __name__ == '__main__':
#     env = Maze()
#     eval_model = Eval_Model(env.n_actions)
#     target_model = Target_Model(env.n_actions)
#     RL = DeepQNetwork(eval_model,target_model,env.n_actions,env.n_features)
#     env.after(100, run_maze)
#     env.mainloop()
#     RL.plot_cost()
if __name__ == "__main__":
    # maze game
    env = Maze()
    RL = DeepQNetwork(env.n_actions, env.n_features,
                      learning_rate=0.01,
                      reward_decay=0.9,
                      e_greedy=0.9,
                      replace_target_iter=50,
                      memory_size=2000,
                      # output_graph=True
                      )
    env.after(100, run_maze)
    env.mainloop()
    RL.plot_cost()
